﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PCBase.UIControls
{
    /// <summary>
    /// 使用软键盘输入。
    /// </summary>
    public class CommonInputEditor : UITypeEditor
    {
        private static frmKeyBoard frmInput;
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if ((context != null) && (provider != null))
            {
                if (frmInput == null)
                {
                    frmInput = new frmKeyBoard();
                }
                string strTemp = (value == null) ? "" : value.ToString();
                frmInput.ShowKeyBoard(new object(), context.PropertyDescriptor.DisplayName, strTemp);
                strTemp = frmInput.DataString;
                try
                {
                    return Convert.ChangeType(strTemp, context.PropertyDescriptor.PropertyType);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            return base.EditValue(context, provider, value);
        }
    }
}
